#! /bin/sh

set -ex

cd mlibc

rm -rf build-$ARCH
mkdir build-$ARCH && cd build-$ARCH

cat <<EOF >cc
#! /bin/sh
$CC $CFLAGS "\$@"
EOF
chmod +x cc
unset CC
unset CFLAGS

cat <<EOF >c++
#! /bin/sh
$CXX $CXXFLAGS "\$@"
EOF
chmod +x c++
unset CXX
unset CXXFLAGS

export PATH="$(pwd -P):$PATH"

cat <<EOF >cross_file.txt
[binaries]
c = 'cc'
cpp = 'c++'

[host_machine]
system = 'v-unikernel'
cpu_family = '$ARCH'
cpu = '$ARCH'
endian = 'little'
EOF
unset ARCH

meson setup .. \
    --cross-file cross_file.txt \
    --buildtype=debugoptimized \
    --prefix="$(pwd -P)"/prefix \
    -Ddefault_library=static \
    -Ddisable_libgcc_dependency=true \
    -Duse_freestnd_hdrs=enabled

ninja -v
ninja install
